<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property Post_model $post_model
 */
class Landing extends MX_Controller
{
    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
    }

    public function index()
    {
        $data = array();
        $this->load->library('Blockcache');
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
		
		if($dataheader = $this->blockcache->get('data-header')) {
        } else {
        	$dataheader = array();
            $this->load->model('option/option_model');
            
			$headerData = $this->option_model->getOptionByName('header');
			$dataheader = $headerData['value'];
           
            $this->blockcache->save('data-header',$dataheader,5000);
        }
		
        if($dataLanding = $this->blockcache->get('data-landing')) {
        } else {
            $this->load->model('category/category_model');
            $this->load->model('post/post_model');
            $this->load->model('option/option_model');
            $lstLanding = $this->option_model->getOptionByName('lstBoxHome');
			
			$headerData = $this->option_model->getOptionByName('header');
			
            $arrLanding = explode(',', $lstLanding['value']);
            $dataLanding = array();
            foreach ($arrLanding as $v) {
                if($tmp = $this->post_model->getPostByCatId($v, '', '', $langCode))
                {
                    $dataLanding[$v]['id'] = $v;
                    $dataLanding[$v]['type'] = $tmp[0]['type'];
                    $dataLanding[$v]['title'] = $tmp[0]['cat_title'];
                    $dataLanding[$v]['post'] = $tmp;
                }
            }
            $this->blockcache->save('data-landing',$dataLanding,5000);
        }
        $this->load->language('landing/landing', $this->_langCode);
        $this->load->helper('text');
        $this->config->load('config_info');
        $configInfo = $this->config->item('infoWebsite');
        $data['siteTitle'] = $configInfo['headTitle'];
        $data['lang'] = $this->lang->line('landing_lang');
        $data['langCode'] = $this->_langCode;
        $data['dataLanding'] = $dataLanding;
		$data['dataHeader'] = $dataheader;
        $this->layout->view('landing/landing_view', $data);
    }
    
    public function change()
    {
        $lang = $this->input->get('lang');
        $backurl = $this->input->get('back_url');
        $this->load->library('Mycommon');
        if($lang) {
            $this->mycommon->setLangCode($lang);
            if($backurl) {
                $back = urldecode($backurl);
                redirect($back);
            } else {
                redirect('trang-chu');
            }
        }
    }

}
?>
